package edu.up.sky.controller;


import edu.up.sky.pojo.User;
import edu.up.sky.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.math.BigInteger;

@Controller
@Slf4j
public class LoginController {

    @Resource
    private UserService userService;

//    @Resource
//    private MailUtil mailUtil;


    @RequestMapping(value = {"/signUp"})
    public String showSignUp() {
        log.info("signUp()");
        return "register";
    }

    @RequestMapping(value = {"/signIn"})
    public String showSignIn() {
        log.info("signIn()");
        return "login";
    }

    @RequestMapping(value = {"/forget"})
    public String forget() {
        log.info("forget()");
        return "forget";
    }

    @RequestMapping(value = {"/index"})
    public String index() {
        log.info("forget()");
        return "index";
    }


    @PostMapping(value = {"/login"})
    public ModelAndView login(ModelAndView mav, BigInteger uid, String password, HttpServletRequest request) {
        User loginUser = userService.login(uid, password);
        if (loginUser != null) {
            // TODO: 账户信息状态保存
            mav.setViewName("index");
        } else {
            mav.addObject("msg", "uid or pwd error");
            mav.setViewName("login");
        }

        return mav;
    }


}
